#ifndef CHAT_H
#define CHAT_H

#include <QWidget>
#include <QtWidgets>
#include <QDebug>

class ChatWidget;
class ChatBubble;

#include "sersocket.h"

#include "localdb.h"
#include "chatbubble.h"
#include "chatbubbleandhead.h"

#define SENDER 1
#define RECEIVER 0

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE



class ChatWidget : public QWidget
{
    Q_OBJECT

public:
    //ChatWidget(QString user_id);//为群聊创建的构造函数，记录登录用户的id
    ChatWidget(Friend sender, Friend receiver, QPixmap senderHead, QPixmap receiverHead, QWidget *parent = nullptr);
    ~ChatWidget();

    void beforeReceiveFile(const QString& fileName,const QByteArray& info,const int& sz);
    void beforeReceivePicture(const QString& fileName,const QByteArray& info,const int& sz);

    void sendMessage(QString msg);
    void receiveMessage(QString msg);
    void sendMessage(Message msg);
    void receiveMessage(Message msg);
    void updateIP(QString ip);

    void sendPicture(const QString& path);
    void receivePicture(const QString& path);



    QScrollArea* scrollarea;
    Ui::Widget *ui;
    QPixmap senderhead;
    QPixmap receivehead;

private slots:

    void on_send_clicked();

    void on_file_button_clicked();

    void update_msgid(const int& id);

    void sendFile(int val, QString fileName);
    void receiveFile(int val, QString fileName);
    void on_history_button_clicked();

public slots:

    void receiveFile_CB2C(const QString& filename,const QByteArray& info,const int& sz);

//    void getUpdateMessage(const QJsonArray &msg_list);



//    void on_icons_button_clicked();;

//    void on_icons_button_2_clicked();

//    void on_picture_button_clicked();

    void on_picture_button_clicked();

signals:
    void Text(const QByteArray& data,const uint16_t cmd);
    void FilePath(const file_box& fbox);
    void RFile(const QString& dir,const QString& fileName,const QByteArray& info,const int& sz,const QString& type);

private:

    QVBoxLayout* playout;
    QWidget* scrollareacontent;

    SerSocket *m_socket;
    // 只是为了判断是否是群聊
    QString user_id;
    Friend sender;
    Friend receiver;

    //ram
    QQueue<Message> msg_que;
    QQueue<file_box> file_que;




protected:
    void resizeEvent(QResizeEvent *event) override;

};
#endif // CHAT_H
